<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= subjectAreaIns();
	$dataArea = sxREQUEST("data_area");
	$subjectArea = sxREQUEST("subject_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
} else {
	if(!isset($_REQUEST["da"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'da' non passata")); 
		serverRedir($url);
	}
	$dataArea = sxREQ('da');
	$subjectArea = "";
	$description = "";
	$note = "";
}
$sSQL = "SELECT description FROM data_area WHERE data_area = '$dataArea'";
$objSDAO->query($sSQL,"ins");
if(!$row = $objSDAO->fetch("ins")) {
	$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'da' non passata")); 
	serverRedir($url);
}
//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			objFrm.submit();
		}catch(e){
			raiseError(e.description, "save new subject area");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form data entry
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"subjectAreaIns.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"data_area\" id=\"data_area\" value=\"$dataArea\">\n");
//output commands
$aCmd = array("conf"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("save new subject area"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>"cancel operation",
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Area Dati").": $dataArea - ".$row['description']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>"._t("codice")." (*)</td>" .
			"<td class=\"text01\" width='10%'><input class=\"inpt01\" type=\"text\" name=\"subject_area\" id=\"subject_area\" maxlength=\"32\" value=\"$subjectArea\" size='30'></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")." (*)</td>" .
			"<td class=\"text01\" colspan='1'><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\" colspan='1'><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("<div id=\"messArea\" class=\"mess01\">$errMsg</div>");
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function subjectAreaIns(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["subject_area"] = strtolower($_REQUEST["subject_area"]);
	if(ereg("[^a-z0-9_]",$_REQUEST["subject_area"]) or $_REQUEST["subject_area"]=='')
		$errMsg .= _t("subject area code - only alphanum and _").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("missing description")."<br>";
	//check existing
	$sSQL = "SELECT count(*) as num FROM subject_area " .
			" WHERE data_area='".sxRAS("data_area")."' " .
				" AND subject_area = '".sxRAS("subject_area")."'";
	$objSDAO->query($sSQL,"ins");
	$row = $objSDAO->fetch("ins");
	if($row["num"] != 0)
		$errMsg .= _t("subject-area existing.<br>");
	if($errMsg != ""){ return $errMsg; }
	$sSQL = "INSERT INTO subject_area " .
			"(data_area, " .
			"subject_area, " .
			"description, " .
			"note)" .
			"VALUES('".sxRAS("data_area")."', " .
			"'".sxRAS("subject_area")."', " .
			"'".sxRAS("description")."', " .
			"'".sxRAS("note")."'" .
			")";	
	
	$objSDAO->query($sSQL,"ins");
	logRecord("300", _t("new subject area '%s'",sxREQUEST('data_area').
		"/".sxREQUEST('subject_area')));
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit;
}
?>
